### PACKAGE PREAMBLE ###

# Create a vector of package names
packages <- c("dplyr", "ggplot2", "tidyr", "tidyverse", "data.table", "readxl", "foreign", "devtools", "fixest", "sandwich", "modelsummary", "kableExtra")

installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
rm(packages, installed_packages)

options(modelsummary_format_numeric_latex = "plain")
f <- function(x) format(x, digits = 3, nsmall = 2, scientific = FALSE)

getwd()
setwd(dir = "/Users/jmc4qg/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder/")
mydata_drop_HOT <- read.dta("Analysis Data/NIBRS_analysis_drop_HOT.dta")
mydata_drop_WARM <- read.dta("Analysis Data/NIBRS_analysis_drop_WARM.dta")
mydata_drop_COLD <- read.dta("Analysis Data/NIBRS_analysis_drop_COLD.dta")

# Panel A: Drop Hot Climates

T1_DH <- feols(homicide_pc~ tMean + prec , data=mydata_drop_HOT)
summary(T1_DH, cluster="fips")
T2_DH <- feols(homicide_pc~ tMean + prec | sample_month + week + dow, data=mydata_drop_HOT)
summary(T2_DH, cluster="fips")
T3_DH <- feols(homicide_pc~ tMean + prec | state_sample_month + week + dow, data=mydata_drop_HOT)
summary(T3_DH, cluster="fips")
T4_DH <- feols(homicide_pc~ tMean + prec | ori_sample_month + week + dow, data=mydata_drop_HOT)
summary(T4_DH, cluster="fips")

models <- list(T1_DH, T2_DH, T3_DH, T4_DH)
modelsummary(models, vcov = ~fips, output = "Figures and Tables/Table_A2/Table_A2_Panel_A.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

# Panel B: Drop Temperate Climates

T1_DW <- feols(homicide_pc~ tMean + prec , data=mydata_drop_WARM)
summary(T1_DW)
T2_DW <- feols(homicide_pc~ tMean + prec | sample_month + week + dow, data=mydata_drop_WARM)
summary(T2_DW, cluster="fips")
T3_DW <- feols(homicide_pc~ tMean + prec | state_sample_month + week + dow, data=mydata_drop_WARM)
summary(T3_DW, cluster="fips")
T4_DW <- feols(homicide_pc~ tMean + prec | ori_sample_month + week + dow, data=mydata_drop_WARM)
summary(T4_DW, cluster="fips")

models <- list(T1_DW, T2_DW, T3_DW, T4_DW)
modelsummary(models, vcov = ~fips, output = "Figures and Tables/Table_A2/Table_A2_Panel_B.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

# Panel C: Drop Cold Climates 

T1_DC <- feols(homicide_pc~ tMean + prec , data=mydata_drop_COLD)
summary(T1_DC)
T2_DC <- feols(homicide_pc~ tMean + prec | sample_month + week + dow, data=mydata_drop_COLD)
summary(T2_DC, cluster="fips")
T3_DC <- feols(homicide_pc~ tMean + prec | state_sample_month + week + dow, data=mydata_drop_COLD)
summary(T3_DC, cluster="fips")
T4_DC <- feols(homicide_pc~ tMean + prec | ori_sample_month + week + dow, data=mydata_drop_COLD)
summary(T4_DC, cluster="fips")

models <- list(T1_DC, T2_DC, T3_DC, T4_DC)
modelsummary(models, vcov = ~fips, output = "Figures and Tables/Table_A2/Table_A2_Panel_C.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

